﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using awsomeSyntezator.PipeLine.Oscilators.Effects;

namespace awsomeSyntezator.PipeLine.Oscilators
{
    class OscilatorPack : Block
    {
        private List<Oscilator> pack;

        public OscilatorPack()
        {
            type = 1;
			pack = new List<Oscilator>();
        }
        public OscilatorPack(Oscilator old)
        {
            type = 1;
            pack.Add(old);
			pack = new List<Oscilator>();
        }

        public void addOscilator(Oscilator neu)
        {
            pack.Add(neu);
        }
        public void generateHarmonics (List<int> sthenghts, float baseFrequency, int type)
        {
            switch(type)
            {
                case 0:
                    for (int i = 0; i< sthenghts.Count; i++)
                    {
						if (sthenghts[i] != 0)
							pack.Add(new SINwawe(baseFrequency * (float)Math.Pow(2, i), Options.rate, sthenghts[i]));
                    }
                    break;
                case 1:
                    for (int i = 0; i < sthenghts.Count; i++)
                    {
						if (sthenghts[i] != 0)
							pack.Add(new SQRwawe(baseFrequency * (float)Math.Pow(2, i), Options.rate, sthenghts[i]));
                    }
					break;
				case 2:
					for (int i = 0; i < sthenghts.Count; i++)
					{
						if (sthenghts[i] != 0)
							pack.Add(new SAWwawe(baseFrequency * (float)Math.Pow(2, i), Options.rate, sthenghts[i]));
					}
					break;
				case 3:
					for (int i = 0; i < sthenghts.Count; i++)
					{
						if (sthenghts[i] != 0)
							pack.Add(new TRIwawe(baseFrequency * (float)Math.Pow(2, i), Options.rate, sthenghts[i]));
					}
					break;
				case 4:
					for (int i = 0; i < sthenghts.Count; i++)
					{
						if (sthenghts[i] != 0)
							pack.Add(new WhiteNoise(baseFrequency * (float)Math.Pow(2, i), sthenghts[i]));
					}
					break;
				case 5:
					for (int i = 0; i < sthenghts.Count; i++)
					{
						if (sthenghts[i] != 0)
							pack.Add(new RedNoise(Options.noiseDensity*(i+1)/10, sthenghts[i]));
					}
					break;
				case 6:
					for (int i = 0; i < sthenghts.Count; i++)
					{
						if (sthenghts[i] != 0)
							pack.Add(new Pulse(baseFrequency * (float)Math.Pow(2, i), sthenghts[i]));
					}
					break;
				case 7:
					for (int i = 0; i < sthenghts.Count; i++)
					{
						if (sthenghts[i] != 0)
							pack.Add(new Wind(0, sthenghts[i]));
					}
					break;
				case 8:
					for (int i = 0; i < sthenghts.Count; i++)
					{
						if (sthenghts[i] != 0)
							pack.Add(new RocketEngine(sthenghts[i]));
					}
					break;
				case 9:
					for (int i = 0; i < sthenghts.Count; i++)
					{
						if (sthenghts[i] != 0)
							pack.Add(new Rain(sthenghts[i]));
					}
					break;
                default:
                    for (int i = 0; i < sthenghts.Count; i++)
                    {
						if (sthenghts[i] != 0)
							pack.Add(new SINwawe(baseFrequency * (float)Math.Pow(2, i), Options.rate, sthenghts[i]));
                    }
                    break;
            }
        }


        public override double getSample(int n, double sample)
        {
            double strenghtsum = 0;
            double tempSample = 0;
            for (int i = 0; i < pack.Count; i++)
            {
                strenghtsum += pack[i].strenght;
            }
            for (int i = 0; i < pack.Count; i++ )
            {
                tempSample += pack[i].getSample(n, 0) * pack[i].strenght /strenghtsum;
            }
            return tempSample;
        }
    }
}
